package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBC {
    private static String url="jdbc:mysql://localhost:3306/zoag";
    private static String root="root";
    private static String password="123456";
    private static Connection connection;
    private static PreparedStatement preparedStatement;
    private static ResultSet resultSet;
    private static int i=0;
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection=DriverManager.getConnection(url,root,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ResultSet getQuery(String sql,Object[] objects){
        try{
             preparedStatement = connection.prepareStatement(sql);
            if (objects.length>0){
                for (int i = 0; i < objects.length; i++) {
                    preparedStatement.setObject(i+1,objects[i]);
                }
            }
             resultSet = preparedStatement.executeQuery();
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }

    public static int getUpdate(String sql,Object[] objects){
        try{
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < objects.length; i++) {
                preparedStatement.setObject(i+1,objects[i]);
            }
            i = preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
}
